package com.neudu.glsb.mapper;

import com.neudu.glsb.pojo.ProductIngredient;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【product_ingredient(商品-小料关联表)】的数据库操作Mapper
* @createDate 2025-09-20 11:25:07
* @Entity com.neudu.glsb.pojo.ProductIngredient
*/
public interface ProductIngredientMapper extends BaseMapper<ProductIngredient> {
    // 根据商品ID获取关联的小料（关联ingredients表获取小料详情）
    @Select("SELECT i.id, i.ingredient_name, i.stock, pi.extra_price " +
            "FROM product_ingredient pi " +
            "LEFT JOIN ingredients i ON pi.ingredient_id = i.id " +
            "WHERE pi.product_id = #{productId} AND i.ingredient_type = '小料' " +
            "ORDER BY pi.is_default DESC")
    List<Map<String, Object>> getIngredientsByProductId(Integer productId);
    // 2. 查询商品已关联的小料ID（用于排除已选小料）
    List<Integer> selectAssignedIngredientIds(@Param("productId") Integer productId);

    // 3. 查询商品可分配的小料（未关联的“小料”类型原料）
    List<Map<String, Object>> selectOptionalIngredients(@Param("assignedIds") List<Integer> assignedIds);
    @Delete("DELETE FROM product_ingredient " +
            "WHERE product_id = #{productId} AND ingredient_id = #{ingredientId}")
    int deleteOne(@Param("productId") Integer productId,
                  @Param("ingredientId") Integer ingredientId);
}




